package logic;

import network.message.MessageConstants;

import java.util.StringTokenizer;

/**
 * Created by IntelliJ IDEA.
 * User: grigory.rozhkov
 * Date: 12.09.12
 * Time: 0:26
 * To change this template use File | Settings | File Templates.
 */
public class UserMessageHandler implements IMessageHandler {
    public String handleMessage(String message) {
        if (message.contains(" ")) {
            int index = message.indexOf(" ");
            String operation = message.substring(0, index);
            if (index == message.length()) return MessageConstants.FAIL;
            String info = message.substring(index + 1, message.length());
            if ((info.charAt(0) != '(') || (info.charAt(info.length() - 1) != ')')) return MessageConstants.FAIL;
            String trimmedInfo = info.substring(1, info.length() - 1);
            StringBuilder stringBuilder = new StringBuilder();
            if (MessageConstants.CREATE.equals(operation)) {
                StringTokenizer stringTokenizer = new StringTokenizer(trimmedInfo, MessageConstants.USER_DELIMITER);
                stringBuilder.append(MessageConstants.CREATE);
                stringBuilder.append(MessageConstants.DELIMITER);
                boolean isId = true;
                while (stringTokenizer.hasMoreTokens()) {
                    stringBuilder.append(stringTokenizer.nextToken());
                    if (isId) {
                        stringBuilder.append(MessageConstants.DELIMITER);
                        isId = false;
                    }
                    else {
                        stringBuilder.append(" ");
                    }
                }
            }
            if (MessageConstants.UPDATE.equals(operation)) {
                StringTokenizer stringTokenizer = new StringTokenizer(trimmedInfo, MessageConstants.USER_DELIMITER);
                stringBuilder.append(MessageConstants.UPDATE);
                stringBuilder.append(MessageConstants.DELIMITER);
                boolean isId = true;
                while (stringTokenizer.hasMoreTokens()) {
                    stringBuilder.append(stringTokenizer.nextToken());
                    if (isId) {
                        stringBuilder.append(MessageConstants.DELIMITER);
                        isId = false;
                    }
                    else {
                        stringBuilder.append(" ");
                    }
                }
            }
            if (MessageConstants.RETRIEVE.equals(operation)) {
                stringBuilder.append(MessageConstants.RETRIEVE);
                stringBuilder.append(MessageConstants.DELIMITER);
                stringBuilder.append(trimmedInfo);
            }
            if (MessageConstants.DELETE.equals(operation)) {
                stringBuilder.append(MessageConstants.DELETE);
                stringBuilder.append(MessageConstants.DELIMITER);
                stringBuilder.append(trimmedInfo);
            }
            if (stringBuilder.toString().equals("")) return MessageConstants.FAIL;
            stringBuilder.append(MessageConstants.DELIMITER + " ");
            stringBuilder.append(MessageConstants.DELIMITER + " ");
            stringBuilder.append(MessageConstants.DELIMITER + " ");
            return stringBuilder.toString();
        }
        return MessageConstants.FAIL;
    }
}
